﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SdlDotNet.Core;
using SdlDotNet.Input;
using SdlDotNet.Graphics;
using Pokemon;
using Point = System.Drawing.Point;

namespace MapEditor
{
    class Input
    {
        public static bool RightMouseDown;
        public static bool RightMousePress;

        public static bool LeftMouseDown;
        public static bool LeftMousePress;

        public static bool MiddleMouseDown;
        public static bool MiddleMousePress;

        public static float WheelMouse = 0;

        public static int MouseX;
        public static int MouseY;

        public static List<Key> KeyDown = new List<Key>();
        public static char Char;
        public static bool Shift;
        public static bool Ctrl;
        public static bool Alt;

        public static void Init()
        {
            Events.MouseMotion += new EventHandler<MouseMotionEventArgs>(MouseMotionEvent);
            Events.MouseButtonDown += new EventHandler<MouseButtonEventArgs>(MouseButtonEvent);
            Events.MouseButtonUp += new EventHandler<MouseButtonEventArgs>(MouseButtonEvent);
            Events.KeyboardDown += new EventHandler<KeyboardEventArgs>(KeyboardEvent);
            Events.KeyboardUp += new EventHandler<KeyboardEventArgs>(KeyboardEvent);
        }

        private static void MouseMotionEvent(object sender, MouseMotionEventArgs e)
        {
            MouseX = e.X;
            MouseY = e.Y;
        }

        private static void MouseButtonEvent(object sender, MouseButtonEventArgs e)
        {
            switch (e.Button)
            {
                case (MouseButton.PrimaryButton):
                    LeftMouseDown = e.ButtonPressed;
                    LeftMousePress = (LeftMousePress ^ LeftMouseDown) && LeftMouseDown;
                    if (LeftMousePress)
                    {
                    }
                    break;
                case (MouseButton.SecondaryButton):
                    RightMouseDown = e.ButtonPressed;
                    RightMousePress = (RightMousePress ^ RightMouseDown) && RightMouseDown;
                    break;
                case (MouseButton.MiddleButton):
                    MiddleMouseDown = e.ButtonPressed;
                    MiddleMousePress = (MiddleMousePress ^ MiddleMouseDown) && MiddleMouseDown;
                    break;
                case (MouseButton.WheelUp):
                    WheelMouse += 0.1f;
                    break;
                case (MouseButton.WheelDown):
                    WheelMouse -= 0.1f;
                    break;
            }
        }

        private static void KeyboardEvent(object sender, KeyboardEventArgs e)
        {
            if (e.Down)
            {
                KeyDown.Add(e.Key);
                Char = e.UnicodeCharacter;
                Ctrl = (e.Mod & ModifierKeys.LeftControl) == ModifierKeys.LeftControl || (e.Mod & ModifierKeys.RightControl) == ModifierKeys.RightControl;
                Shift = (e.Mod & ModifierKeys.LeftShift) == ModifierKeys.LeftShift || (e.Mod & ModifierKeys.RightShift) == ModifierKeys.RightShift;
                Alt = (e.Mod & ModifierKeys.LeftAlt) == ModifierKeys.LeftAlt || (e.Mod & ModifierKeys.RightAlt) == ModifierKeys.RightAlt;
            }
            else
            {
                KeyDown.Remove(e.Key);
                Char = (char)0;
                Ctrl = false;
                Shift = false;
                Alt = false;
            }
        }
    }
}
